package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.wemedia.client.WemediaFeign;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-26
 */
@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Autowired
    private WemediaFeign feign;

    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    public PageResult<AdSensitive> findByPage(SensitiveDto dto) {
        // 构造分页条件
        PageResult<SensitiveDto> feignByPage = feign.findByPage(dto);

        List<AdSensitive> adSensitive = BeanHelper.copyWithCollection(feignByPage.getData(), AdSensitive.class);

        return new PageResult<AdSensitive>(dto.getPage(),dto.getSize(),feignByPage.getTotal(),adSensitive);
    }


    /**
     * 添加敏感词
     *
     * @param adSensitive
     * @return
     */
    public void saveSensitive(AdSensitive adSensitive) {
        SensitiveDto sensitiveDto = new SensitiveDto();
        sensitiveDto.setSensitives(adSensitive.getSensitives());
        sensitiveDto.setId(adSensitive.getId());

        boolean b =  feign.saveSensitive(sensitiveDto);

        if (!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    public void updateByIdSensitive(SensitiveDto dto) {
        feign.updateById(dto);
    }

    /**
     * 删除敏感词
     *
     * @param
     * @return
     */
    public void deleteById( Integer id) {
        boolean b = feign.deleteById(id);
        if (!b){
            log.error("远端调用删除失败...");

            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}
